package com.taptech.doufu.util;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import androidx.recyclerview.widget.RecyclerView;
import android.widget.BaseAdapter;

import com.taptech.doufu.bean.BaseBean;
import com.taptech.doufu.bean.MineAbstractBean;
import com.taptech.doufu.constant.Constant;

import com.taptech.doufu.listener.HttpResponseListener;
import com.taptech.doufu.services.PersonalNoteService;
import com.taptech.doufu.services.MangerService;
import com.taptech.doufu.net.httputils.HttpResponseObject;
import com.taptech.doufu.ui.view.WaitDialog;

import java.util.List;

public class ListViewUtil {


    static List<? extends BaseBean> mDataList;

    /**
     * 取消内容收藏
     *
     * @param context
     * @param mDialog
     * @param dataList
     * @param adapter
     * @param position
     * @param id
     * @param object_type
     */

    public static synchronized <T extends BaseBean> void deleteItemCollect(final Context context, final WaitDialog mDialog, List<T> dataList, final RecyclerView.Adapter adapter, final int position, final String id, final String object_type)
    {
        mDataList = dataList;
        new AlertDialog.Builder(context).setTitle("确定取消收藏").setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                deleteItemCollectRequest(context, id, object_type, adapter, position, mDialog);
            }
        }).show();
    }

    private static <T extends BaseBean> void deleteItemCollectRequest(final Context context, final String id, final String object_type, final RecyclerView.Adapter adapter, final int mDelPosition, final WaitDialog dialog)
    {
        final HttpResponseListener httpResponseListener = new HttpResponseListener()
        {
            @Override
            public void handleResponse(int handleType, HttpResponseObject response)
            {
                if (handleType == MangerService.HANDLE_TYPE_DELETE_COLLECT)
                {
                    if (dialog != null && dialog.isShowing())
                    {
                        dialog.dismiss();
                    }
                    if (response.getStatus() == 0)
                    {
                        try
                        {
                            mDataList.remove(mDelPosition);
                            adapter.notifyDataSetChanged();
                        } catch (IndexOutOfBoundsException e)
                        {
                        } catch (NullPointerException e)
                        {
                        }
                    } else
                    {
                        if (response.getUser_msg() != null)
                        {
                            UIUtil.toastMessage(context, response.getUser_msg());
                        } else
                        {
                            UIUtil.toastMessage(context, Constant.loadingFail);
                        }
                    }
                }
            }
        };
        // 由于话题和小说在收藏内用的同一个type16 但展现数据里小说type18话题是16
        MangerService.getInstance().deleteObjectCollect(id, object_type, httpResponseListener);

        if (dialog != null)
        {

            dialog.show();
        }

    }

    public static synchronized <T extends BaseBean> void deleteItemCollect(final Context context, final WaitDialog mDialog, List<T> dataList, final BaseAdapter adapter, final int position, final String id, final String object_type)
    {
        mDataList = dataList;
        new AlertDialog.Builder(context).setTitle("确定取消收藏").setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                deleteItemCollectRequest(context, id, object_type, adapter, position, mDialog);
            }
        }).show();
    }

    private static <T extends BaseBean> void deleteItemCollectRequest(final Context context, final String id, final String object_type, final BaseAdapter adapter, final int mDelPosition, final WaitDialog dialog)
    {
        final HttpResponseListener httpResponseListener = new HttpResponseListener()
        {
            @Override
            public void handleResponse(int handleType, HttpResponseObject response)
            {
                if (handleType == MangerService.HANDLE_TYPE_DELETE_COLLECT)
                {
                    if (dialog != null && dialog.isShowing())
                    {
                        dialog.dismiss();
                    }
                    if (response.getStatus() == 0)
                    {
                        try
                        {
                            mDataList.remove(mDelPosition);
                            adapter.notifyDataSetChanged();
                        } catch (IndexOutOfBoundsException e)
                        {
                        } catch (NullPointerException e)
                        {
                        }
                    } else
                    {
                        if (response.getUser_msg() != null)
                        {
                            UIUtil.toastMessage(context, response.getUser_msg());
                        } else
                        {
                            UIUtil.toastMessage(context, Constant.loadingFail);
                        }
                    }
                }
            }
        };
        // 由于话题和小说在收藏内用的同一个type16 但展现数据里小说type18话题是16
        MangerService.getInstance().deleteObjectCollect(id, object_type, httpResponseListener);

        if (dialog != null)
        {

            dialog.show();
        }

    }



    public static synchronized void deleteItem(Context context, final List<MineAbstractBean> dataList, final RecyclerView.Adapter adapter, final int position, final String id, final int object_type)
    {
        new AlertDialog.Builder(context, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT).setTitle("确定删除").setNegativeButton("取消", null).setPositiveButton("确定", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                deleteItemRequest(id, object_type, adapter, dataList, position);
            }
        }).show();
    }

    private static void deleteItemRequest(final String id, final int object_type, final RecyclerView.Adapter adapter, final List<MineAbstractBean> dataList, final int mDelPosition)
    {
        HttpResponseListener httpResponseListener = new HttpResponseListener()
        {
            @Override
            public void handleResponse(int handleType, HttpResponseObject response)
            {
                dataList.remove(mDelPosition);
                adapter.notifyDataSetChanged();
            }
        };

        switch (object_type)
        {
            case Constant.OBJECT_TYPE_USERS_TOPIC:
                MangerService.getInstance().mangerDelTopic(id, httpResponseListener);
                break;
            case Constant.OBJECT_TYPE_SWEEP:
                MangerService.getInstance().mangerDelSweep(id, httpResponseListener);
                break;
            case Constant.OBJECT_TYPE_NOTE:
                PersonalNoteService.getInstance().deletePersonalNote(httpResponseListener, id);
                break;
            case Constant.OBJECT_TYPE_DRAW:
                PersonalNoteService.getInstance().deletePersonalNote(httpResponseListener, id);
                break;
            case Constant.OBJECT_TYPE_COS:
                PersonalNoteService.getInstance().deletePersonalNote(httpResponseListener, id);
                break;


        }
    }
}